﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;
using System.Drawing;

namespace C12_Ex02_Shaked_032675316_Itay_032511057
{
    public class FaceBookGalleriesDownloadHandler : IFaceBookGalleriesDownloadHandler
    {
        private static IFaceBookGalleriesDownloadHandler m_Instance;

        private FaceBookGalleriesDownloadHandler()
        {
        }

        public static IFaceBookGalleriesDownloadHandler Instance
        {
            get 
            {
                if (m_Instance == null)
                {
                    m_Instance = new FaceBookGalleriesDownloadHandler();
                }

                return m_Instance;
            }
        }

        public void DownloadImage(string i_Photo, string i_AlbumName, string i_DownloadFolder)
        {
            string directoryPath = i_DownloadFolder + Path.DirectorySeparatorChar + i_AlbumName.SafeFileNameString();
            string filenameFromUrl = i_Photo.Split('/').Last();
            string fileFullPath = directoryPath + Path.DirectorySeparatorChar + filenameFromUrl.SafeFileNameString();
            if (!File.Exists(fileFullPath))
            {
                Directory.CreateDirectory(directoryPath);
                try
                {
                    WebRequest req = WebRequest.Create(i_Photo);
                    WebResponse response = req.GetResponse();
                    Stream stream = response.GetResponseStream();
                    Image.FromStream(stream).Save(fileFullPath);
                }
                catch
                {
                }
            }
        }
    }
}
